<template>
  <a-table
    :columns="columns"
    :data-source="data"
    :pagination="ipagination"
    @change="pageChange"
    bordered
    :rowKey="(record) => record.person_id"
  >
    <template slot="operation" slot-scope="text, record">
      <div class="editable-row-operations">
        <span>
          <a-popconfirm
            v-if="data.length"
            title="确定要删除吗?"
            @confirm="() => onDelete(record)"
          >
            <a :disabled="editingKey !== ''">删除</a>
          </a-popconfirm>
        </span>
      </div>
    </template>
  </a-table>
</template>

<script>
import { request } from "../../../api/index";
const columns = [
  {
    title: "工号",
    dataIndex: "person_id",
    width: "25%",
    scopedSlots: { customRender: "person_id" },
  },
  {
    title: "姓名",
    dataIndex: "name",
    width: "25%",
    scopedSlots: { customRender: "name" },
  },
  {
    title: "性别",
    dataIndex: "sex",
    width: "25%",
    scopedSlots: { customRender: "sex" },
    customRender: (text) => {
      if (text == 0) {
        return "男";
      } else {
        return "女";
      }
    },
  },
  {
    title: "操作",
    dataIndex: "operation",
    width: "25%",
    scopedSlots: { customRender: "operation" },
  },
];

const data = [];

export default {
  inject: ["reload"],
  name:"teacherContent",
  data() {
    return {
      data: [],
      columns,
      editingKey: "",
      ipagination: {
        current: 1,
        pageSize: 8,
        total: data.length,
        showSizeChanger: true,
        showQuickJumper: true,
        hideOnSinglePage: true, // 少于一页时隐藏分页
        pageSizeOptions: ["8", "16"], //这里注意只能是字符串，不能是数字
        showTotal: (total, range) =>
          `显示${range[0]}-${range[1]}条，共有 ${total}条`,
      },
    };
  },
  mounted() {
    request({
      url: "/user/teacher_view",
      method: "get",
    })
      .then((res) => {
        // console.log(res);
        this.data = res.data.teachers;
      })
      .catch((err) => {
        // console.log(err);
      });
  },
  methods: {
    refresh(){
      // console.log("刷新");
      this.reload();
    },
    pageChange(page, pageSize) {
      this.ipagination.current = page.current;
      this.ipagination.pageSize = page.pageSize;
    },
    onDelete(key) {
      // console.log(key.person_id);
      request({
        url: "/user/teacher_delete",
        method: "post",
        data: {
          id: key.person_id,
        },
      })
        .then((res) => {
          // console.log(res);
          if (res.data.message == "success") {
            alert("删除成功");
          } else {
            alert("删除失败");
          }
        })
        .catch((err) => {
          // console.log(err);
        });
      this.reload();
    },
  },
};
</script>
